﻿using System.Text;
using System.Linq;
using System.Collections.Generic;
using Eagles.Base.DataBase;
using Eagles.Application.Model.Common;
using Eagles.Application.Model.Enums;
using Eagles.DomainService.Model.App;
using Eagles.DomainService.Model.News;
using Eagles.Interface.DataAccess.NewsDa;

namespace Ealges.DomianService.DataAccess.NewsDA
{
    public class NewsDa : INewsDa
    {
        private readonly IDbManager dbManager;

        public NewsDa(IDbManager dbManager)
        {
            this.dbManager = dbManager;
        }

        public TbAppModule GetSubCateType(int moduleId, int appId)
        {
            return dbManager.QuerySingle<TbAppModule>(@"select OrgId,ModuleId,SubCateType from eagles.tb_app_module where ModuleId=@ModuleId and OrgId=@OrgId ", new { ModuleId = moduleId, OrgId = appId });
        }

        public List<TbNews> GetModuleNews(int moduleId, int appId, bool isHome, int pageIndex = 1, int pageSize = 10, int branchId = 0)
        {
            int pageIndexParameter = (pageIndex - 1) * pageSize;
            var sql = new StringBuilder(@" select OrgId,NewsId,ShortDesc,Title,HtmlContent,Author,Source,Module,Status,BeginTime,EndTime,TestId,
Attach1,Attach2,Attach3,Attach4,Attach5,OperId,CreateTime,IsImage,IsVideo,IsAttach,IsClass,IsLearning,IsText,ViewCount,ReviewId,CanStudy,ImageUrl,IsExternal,ExternalUrl
from eagles.tb_news where Module=@Module and OrgId = @OrgId and Status = 0");
            if (isHome)
            {
                sql.Append(" and ImageUrl is not null and ImageUrl <> '' ");
            }

            if (branchId > 0)
            {
                sql.AppendFormat(" and (branchId = 0 or branchId = {0} or IsPublicOrg = 1 )", branchId);
            }
            else
            {
                sql.Append(" and (branchId = 0 or IsPublicOrg = 1) ");
            }

            sql.Append(" order by IsTop desc,CreateTime desc limit @PageIndex, @PageSize ");
            return dbManager.Query<TbNews>(sql.ToString(), new { Module = moduleId, OrgId = appId, PageIndex = pageIndexParameter, PageSize = pageSize });
        }

        public TbNews GetNewsDetail(int newsId, int appId)
        {
            return dbManager.QuerySingle<TbNews>(@"select OrgId,NewsId,ShortDesc,Title,HtmlContent,Author,Source,Module,Status,BeginTime,EndTime,TestId,Attach1,Attach2,Attach3,Attach4,
AttachName1,AttachName2,AttachName3,AttachName4,Attach5,OperId,CreateTime,IsImage,IsVideo,IsAttach,IsClass,IsLearning,IsText,ViewCount,ReviewId,CanStudy,ImageUrl,
IsExternal,ExternalUrl,StudyTime,RewardsScore from eagles.tb_news where NewsId=@NewsId and OrgId=@OrgId and Status = 0 ", new { NewsId = newsId, OrgId = appId });
        }

        public int AddNewsViewCount(int type, int newsId)
        {
            switch (type)
            {
                //新闻
                case 0: return dbManager.Excuted("update eagles.tb_news set ViewCount = ifnull(ViewCount,0) + 1 where NewsId=@NewsId ", new { NewsId = newsId });
                //任务
                case 1: return dbManager.Excuted("update eagles.tb_task set ViewCount = ifnull(ViewCount,0) + 1 where TaskId=@NewsId ", new { NewsId = newsId });
                //活动
                case 2: return dbManager.Excuted("update eagles.tb_activity set ViewCount = ifnull(ViewCount,0) + 1 where ActivityId=@NewsId ", new { NewsId = newsId });
                //用户文章
                case 3: return dbManager.Excuted("update eagles.tb_user_news set ViewCount = ifnull(ViewCount,0) + 1 where NewsId=@NewsId ", new { NewsId = newsId });
            }
            return 0;
        }

        public List<JoinPeople> GetMeetingJoinPeople(int newsId)
        {
            var result = dbManager.Query<JoinPeople>(@"select a.UserId, b.Name from eagles.tb_meeting_user a 
join eagles.tb_user_info b on a.UserId = b.UserId where NewsId = @NewsId ", new { NewsId = newsId });
            if (result != null && result.Any())
                return result;
            return null;
        }

        public List<TbNews> GetMeetings(MeetEnum meetType, int userId, int appId, int pageIndex = 1, int pageSize = 10)
        {
            int pageIndexParameter = (pageIndex - 1) * pageSize;
            if (meetType == MeetEnum.Join)
            {
                return dbManager.Query<TbNews>(@"select a.OrgId,a.NewsId,Title,HtmlContent,Author,CreateTime,ImageUrl,Author from eagles.tb_news a 
join eagles.tb_meeting_user b on a.NewsId = b.NewsId where b.UserId = @UserId and a.OrgId = @OrgId and NewsType = 1 and Status = 0 order by CreateTime desc limit @PageIndex, @PageSize ",
                new { UserId = userId,OrgId = appId, PageIndex = pageIndexParameter, PageSize = pageSize});
            }
            else
            {
                return dbManager.Query<TbNews>(@"select a.OrgId,a.NewsId,Title,HtmlContent,Author,CreateTime,ImageUrl,Author from eagles.tb_news a 
join eagles.tb_meeting_user b on a.NewsId = b.NewsId where b.UserId <> @UserId and a.OrgId = @OrgId and NewsType = 1 and Status = 0 order by CreateTime desc limit @PageIndex, @PageSize ",
                new { UserId = userId,OrgId = appId, PageIndex = pageIndexParameter, PageSize = pageSize});
            }
        }

        public int GetMeetingsCount(MeetEnum meetType, int userId, int appId)
        {
            if (meetType == MeetEnum.Join)
            {
                return dbManager.ExecuteScalar<int>(@"select Count(*) from eagles.tb_news a join eagles.tb_meeting_user b on a.NewsId = b.NewsId where b.UserId = @UserId and a.OrgId = @OrgId and NewsType = 1 and Status = 0",
                    new { UserId = userId, OrgId = appId });
            }
            else
            {
                return dbManager.ExecuteScalar<int>(@"select Count(*) from eagles.tb_news a join eagles.tb_meeting_user b on a.NewsId = b.NewsId where b.UserId <> @UserId and a.OrgId = @OrgId and NewsType = 1 and Status = 0 ",
                    new { UserId = userId, OrgId = appId });
            }
        }

        public TbNews GetMeetingDetail(int newsId, int appId)
        {
            return dbManager.QuerySingle<TbNews>(@"select OrgId,NewsId,Title,HtmlContent,Author,CreateTime,ImageUrl,Author,Attach1,Attach2,Attach3,Attach4,Attach5,AttachName1,AttachName2,AttachName3,AttachName4
from eagles.tb_news where NewsId=@NewsId and NewsType = 1 and OrgId=@OrgId and Status = 0 ", new { NewsId = newsId, OrgId = appId });
        }
    }
}